﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

using Microsoft.SharePoint.Client;

namespace SharePointOnlineServices
{
    public class SharePointContextProvider
    {
        public ClientContext CreateSharePointContext(string sitePath)
        {
            var siteUrl = ConfigurationManager.AppSettings["sharePointUrl"];
            var serviceAccountUser = ConfigurationManager.AppSettings["serviceAccountUser"];
            var serviceAccountPassword = ConfigurationManager.AppSettings["serviceAccountPassword"];

            var securePassword = new System.Security.SecureString();

            foreach (var c in serviceAccountPassword)
                securePassword.AppendChar(c);

            var context = new ClientContext(new Uri(siteUrl).GetLeftPart(UriPartial.Authority) + sitePath);
            context.Credentials = new SharePointOnlineCredentials(serviceAccountUser, securePassword);

            return context;
        }
    }
}